package com.huitone.gddw.service.impl.system;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huitone.gddw.dao.entity.RoleMenu;
import com.huitone.gddw.dao.system.IRoleMenuDao;
import com.huitone.gddw.service.system.IRoleMenuService;
import com.huitone.gddw.utils.StringUtils;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 * 角色访问功能管理 接口实现类
 * @author wuluodan
 *
 */
@Service
public class RoleMenuService implements IRoleMenuService {

	@Autowired
	private IRoleMenuDao roleMenuDao;
	
	@Override
	public List<RoleMenu> getByRoleId(String roleId) {
		return roleMenuDao.getByRoleId(roleId);
	}

	@Override
	public void changeRoleMenus(String roleId, String menuIds) {
		List<String> list = StringUtils.isBlank(menuIds) ? new ArrayList<String>() : Arrays.asList(menuIds.split(","));
		List<String> listSave = new ArrayList<String>();
		List<String> listDel = new ArrayList<String>();
		List<String> listAll = this.getMenuIdsByRoleId(roleId);
		for (String s : list) {
			if (!listAll.contains(s)) {
				listSave.add(s);
			}
		}
		for (String s : listAll) {
			if (!list.contains(s)) {
				listDel.add(s);
			}
		}
		this.updateByRoleId(roleId, listSave, listDel);
	}
	
	/**
	 * 根据用户ID保存/删除角色权限
	 * @param userId 用户ID
	 * @param listSave 保存列表
	 * @param listDel 删除列表
	 */
	private void updateByRoleId(String roleId, List<String> listSave, List<String> listDel) {
		List<RoleMenu> list = new ArrayList<RoleMenu>();
		for (String s : listSave) {
			RoleMenu entity = new RoleMenu();
			entity.setRoleId(roleId);
			entity.setMenuId(s);
			list.add(entity);
		}
		this.saveBatch(list);
		this.deleteByRoleId(roleId, listDel);
	}
	
	/**
	 * 批量保存
	 * @param list
	 */
	private void saveBatch(List<RoleMenu> list) {
		for (RoleMenu roleMenu : list) {
			roleMenu.setRecId(StringUtils.uuid());
			roleMenu.setInsertTime(new Date());
			roleMenu.setInsertStaffId(SysLoginUtil.getLoginUserId());
			roleMenu.setLastModTime(roleMenu.getInsertTime());
			roleMenu.setLastModStaffId(roleMenu.getInsertStaffId());
		}
		if (!list.isEmpty() && list.size() > 0) {
			roleMenuDao.insertBatch(list);
		}
	}
	
	/**
	 * 批量删除
	 * @param roleId 角色ID
	 * @param listDel
	 */
	private void deleteByRoleId(String roleId, List<String> listDel) {
		if (!listDel.isEmpty() && listDel.size() > 0) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("roleId", roleId);
			map.put("list", listDel);
			roleMenuDao.deleteByRoleId(map);
		}
	}
	
	/**
	 * 获取角色所有菜单ID
	 * @param roleId 角色ID
	 * @return List
	 */
	private List<String> getMenuIdsByRoleId(String roleId) {
		List<String> listId = new ArrayList<String>();
		List<RoleMenu> list = this.getByRoleId(roleId);
		for (RoleMenu roleMenu : list) {
			listId.add(roleMenu.getMenuId());
		}
		return listId;
	}

	@Override
	public void delAllByRoleId(String roleId) {
		roleMenuDao.delAllByRoleId(roleId);
	}

	@Override
	public List<String> getMenuIdByRoleId(String roleId) {
		List<String> l = new ArrayList<String>();
		List<RoleMenu> list = this.getByRoleId(roleId);
		for (RoleMenu roleMenu : list) {
			l.add(roleMenu.getMenuId());
		}
		return l;
	}

}
